/**   
* @Title: NurseTrainingServiceImpl.java  
* @Package com.web.exiaodao.business.refund_service.impl
* @Description: TODO(用一句话描述该文件做什么)  
* @author huangyang    
* @date 2016年12月20日 上午10:44:15      
*/  
package com.web.exiaodao.business.service.impl;

import java.util.Map;

import com.web.exiaodao.business.entity.ServiceNurseTraining;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;
import com.web.exiaodao.business.repository.ServiceNurseTrainingRepository;
import com.web.exiaodao.business.service.NurseTrainingService;

/**
* <p>Title:NurseTrainingServiceImpl </p>
* <p>Description: </p>
* <p>Company:</p> 
* @author huang
* @date 2016年12月20日 上午10:44:15
*/
@Service("NurseTrainingService")
@Transactional(readOnly = false)
public class NurseTrainingServiceImpl extends BaseService<ServiceNurseTraining> implements NurseTrainingService {

	private static final Logger LOGGER = Logger.getLogger(NurseTrainingServiceImpl.class);
	
	@Autowired
	private ServiceNurseTrainingRepository nurseTrainingRepository;
	
	/**
	 * 查询单条NurseTraining信息
	 * //@param tId
	 * @return 返回单个NurseTraining对象
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<ServiceNurseTraining> findOne(Long modelId) throws BusinessException {
		ServiceNurseTraining obj = new ServiceNurseTraining();
		if(modelId != 0){
			obj = nurseTrainingRepository.findByIdAndStatusNot(modelId,XaConstant.Status.delete);
		}
		XaResult<ServiceNurseTraining> xr = new XaResult<ServiceNurseTraining>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(obj);
			LOGGER.info("后台----->查询单个实体----->ServiceNurseTraining");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	/**
	 * 分页查询状态非status的NurseTraining数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象NurseTraining集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<ServiceNurseTraining>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<ServiceNurseTraining> page = nurseTrainingRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ServiceNurseTraining.class), pageable);
		XaResult<Page<ServiceNurseTraining>> xr = new XaResult<Page<ServiceNurseTraining>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态不等于）----->ServiceNurseTraining");
		return xr;
	}

	/**
	 * 分页查询状态status的NurseTraining数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象NurseTraining集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<ServiceNurseTraining>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<ServiceNurseTraining> page = nurseTrainingRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ServiceNurseTraining.class), pageable);
		XaResult<Page<ServiceNurseTraining>> xr = new XaResult<Page<ServiceNurseTraining>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态等于）----->ServiceNurseTraining");
		return xr;
	}

	/**
	 * 保存NurseTraining信息
	 * @param model
	 * @return
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<ServiceNurseTraining> saveOrUpdate(ServiceNurseTraining model) throws BusinessException {
		ServiceNurseTraining obj = null;
		if(XaUtil.isNotEmpty(model.getId())){
			obj = nurseTrainingRepository.findOne(model.getId());
		}else{
			obj = new ServiceNurseTraining();
		}
	    obj.setNurseTraining_Name(XaUtil.isNotEmpty(model.getNurseTraining_Name()) ? model.getNurseTraining_Name() : obj.getNurseTraining_Name());
	    obj.setNurseTraining_StartTime(XaUtil.isNotEmpty(model.getNurseTraining_StartTime()) ? model.getNurseTraining_StartTime() : obj.getNurseTraining_StartTime());
	    obj.setNurseTraining_EndTime(XaUtil.isNotEmpty(model.getNurseTraining_EndTime()) ? model.getNurseTraining_EndTime() : obj.getNurseTraining_EndTime());
	    obj.setNurseTraining_Address(XaUtil.isNotEmpty(model.getNurseTraining_Address()) ? model.getNurseTraining_Address() : obj.getNurseTraining_Address());
	    obj.setNurseTraining_organization(XaUtil.isNotEmpty(model.getNurseTraining_organization()) ? model.getNurseTraining_organization() : obj.getNurseTraining_organization());
	    obj.setQuantity(XaUtil.isNotEmpty(model.getQuantity()) ? model.getQuantity() : obj.getQuantity());
	    obj.setPrice(XaUtil.isNotEmpty(model.getPrice()) ? model.getPrice() : obj.getPrice());
	    obj.setQuantityInventory(XaUtil.isNotEmpty(model.getQuantityInventory()) ? model.getQuantityInventory() : obj.getQuantityInventory());
	    obj.setOrderStartTime(XaUtil.isNotEmpty(model.getOrderStartTime()) ? model.getOrderStartTime() : obj.getOrderStartTime());
	    obj.setOrderEndTime(XaUtil.isNotEmpty(model.getOrderEndTime()) ? model.getOrderEndTime() : obj.getOrderEndTime());
	    obj.setNurseTraining_Content(XaUtil.isNotEmpty(model.getNurseTraining_Content()) ? model.getNurseTraining_Content() : obj.getNurseTraining_Content());
	    obj.setNurseTraining_url(XaUtil.isNotEmpty(model.getNurseTraining_url()) ? model.getNurseTraining_url() : obj.getNurseTraining_url());
	    obj.setMobile(XaUtil.isNotEmpty(model.getMobile()) ? model.getMobile() : obj.getMobile());
	    obj.setSort(XaUtil.isNotEmpty(model.getSort()) ? model.getSort() : obj.getSort());
	    obj = nurseTrainingRepository.save(obj);
		XaResult<ServiceNurseTraining> xr = new XaResult<ServiceNurseTraining>();
		xr.setObject(obj);
		LOGGER.info("后台新增或编辑数据成功！");
		return xr;
	}

	/**
	 * 修改NurseTraining状态，可一次修改多条   3删除  -1锁定  1正常
	 * //@param userId
	 * @param modelIds
	 * @param status
	 * @return 返回NurseTraining对象
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<ServiceNurseTraining> multiOperate(
			String modelIds,Integer status) throws BusinessException {
		XaResult<ServiceNurseTraining> xr = new XaResult<ServiceNurseTraining>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				ServiceNurseTraining obj = nurseTrainingRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = nurseTrainingRepository.save(obj);
				}
			}
		}
		LOGGER.info("批量修改数据状态成功！");
		return xr;
	}

}
